package com.moyu.myadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import  com.moyu.myadmin.model.dto.EmpLeaveDTO;
import  com.moyu.myadmin.model.vo.EmpLeaveVO;
import  com.moyu.myadmin.service.EmpLeaveService;
import  com.moyu.myadmin.utils.QueryData;
import  com.moyu.myadmin.utils.ResultData;
import  com.moyu.myadmin.utils.ReturnCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * 员工请假
 *
 * @email jiaomingyu5778@gmail.com
 * @date 2022-02-25 15:09:13
 */
@Validated
@Log4j2
@Api(tags = {"员工请假接口"})
@RestController
@RequestMapping("/empLeave/")
public class EmpLeaveController {

    private final EmpLeaveService service;

    @Autowired
    public EmpLeaveController(EmpLeaveService service) {
        this.service = service;
    }

    @ApiOperation(value = "分页查询员工请假信息")
    @PostMapping("queryListPage")
    public ResultData<Page<EmpLeaveVO>> queryList(@RequestBody QueryData<EmpLeaveDTO> queryData) {
        log.info("员工请假查询，查询信息：{}", queryData);
        Page<EmpLeaveVO> page = service.queryListPage(queryData);
        return ResultData.success(page);
    }

    @ApiOperation(value = "保存员工请假信息")
    @PostMapping("save")
    public ResultData<String> save(@ApiParam(value = "员工请假信息") @Valid @RequestBody EmpLeaveDTO dto) {
        log.info("员工请假保存，保存信息：{}", dto);
        boolean update = service.save(dto);
        return update ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "发起员工请假流程")
    @PostMapping("startProcess")
    public ResultData<String> startProcess(@ApiParam(value = "员工请假信息") @Valid @RequestBody EmpLeaveDTO dto) {
        log.info("员工请假保存，保存信息：{}", dto);
        boolean update = service.startProcess(dto);
        return update ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "根据ID，批量删除员工请假信息")
    @PostMapping("/deletes")
    public ResultData<String> deletes(@Valid @NotNull @Size(min = 1, message = "主键长度最小为1") @RequestBody List<String> leaveIds) {
        log.info("员工请假批量删除，删除信息ID：{}", leaveIds);
        boolean remove = service.removeBatchByIds(leaveIds);
        return remove ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }

    @ApiOperation(value = "根据ID，删除员工请假信息")
    @PostMapping("/delete")
    public ResultData<String> delete(@Valid @NotEmpty(message = "ID不能为空") @RequestBody String leaveId) {
        log.info("员工请假删除，删除信息ID：{}", leaveId);
        boolean remove = service.removeById(leaveId);
        return remove ? ResultData.success(ReturnCode.RC200.getMessage()) : ResultData.error(ReturnCode.RC999.getMessage());
    }



}
